package com.hbhmzy.utils;



import android.util.Log;

import java.net.DatagramPacket;
import java.net.DatagramSocket;


public class UDPUtils {
    private static final String TAG = UDPUtils.class.getSimpleName();

    public static boolean send(byte[] d, int dataLen) {
        final DatagramSocket skt = LocalUDPSocketProvider.getInstance().getLocalUDPSocket();

        if (skt != null && d != null) {
            try {
                DatagramPacket p = new DatagramPacket(d, dataLen);
                boolean sendSucess = true;
                if (skt.isConnected())// 只有前面调用了connect方法后，isConnected才会返回true哦
                {
                    try {
                        skt.send(p);
                    } catch (Exception e) {
                        sendSucess = false;
                        Log.e(TAG, "【IMCORE】send方法中》》发送UDP数据报文时出错了，原因是：" + e.getMessage());
                    }
                }
                return sendSucess;
            } catch (Exception e) {
                Log.e(TAG, "【IMCORE】send方法中》》发送UDP数据报文时出错了：remoteIp=" + skt.getInetAddress()
                        + ", remotePort=" + skt.getPort() + ".原因是：" + e.getMessage());
                return false;
            }
        } else {
            Log.e(TAG, "【IMCORE】send方法中》》无效的参数：skt=" + skt);;//
            return false;
        }
    }


}